﻿using System;
using System.Collections.Generic;
using System.Reflection;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace HYCoordinate
{
	public class DrainBuryData
	{
		public static Schema CreatSchema(Document doc, string strSchemaGuid, string strSchemaName)
		{
			Guid guid = new Guid(strSchemaGuid);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				Transaction transaction = new Transaction(doc, "CreateDrainSchema");
				try
				{
					transaction.Start();
					SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
					Type typeFromHandle = typeof(DrainBuryDataStruct);
					schemaBuilder.SetSchemaName(strSchemaName);
					foreach (FieldInfo fieldInfo in typeFromHandle.GetFields())
					{
						Type fieldType = fieldInfo.FieldType;
						if (fieldType == typeof(string))
						{
							schemaBuilder.AddSimpleField(fieldInfo.Name, typeof(string));
						}
						else if (fieldType == typeof(List<string>))
						{
							schemaBuilder.AddArrayField(fieldInfo.Name, typeof(string));
						}
					}
					schema = schemaBuilder.Finish();
					transaction.Commit();
				}
				catch (Exception)
				{
					transaction.RollBack();
				}
			}
			return schema;
		}

		public static bool BuryDataToDrain(string inputGuid, Group DrainGroup, string strSchemaName, DrainBuryDataStruct dataStruct)
		{
			Guid guid = new Guid(inputGuid);
			Document document = DrainGroup.Document;
			Schema schema = Schema.Lookup(guid);
			Type typeFromHandle = typeof(DrainBuryDataStruct);
			Transaction transaction = new Transaction(document, "WriteBuryDataOfDrain");
			Entity entity = null;
			if (schema != null && schema.SchemaName.Equals(strSchemaName))
			{
				if (entity == null)
				{
					entity = new Entity(schema);
				}
				try
				{
					transaction.Start();
					foreach (FieldInfo fieldInfo in typeFromHandle.GetFields())
					{
						Field field = schema.GetField(fieldInfo.Name);
						Type fieldType = fieldInfo.FieldType;
						if (fieldType.Name.Equals(typeof(string).Name))
						{
							entity.Set<string>(field, (string)fieldInfo.GetValue(dataStruct));
						}
						else if (fieldType.Name.Equals(typeof(List<string>).Name))
						{
							entity.Set<IList<string>>(field, fieldInfo.GetValue(dataStruct) as IList<string>);
						}
					}
					DrainGroup.SetEntity(entity);
					transaction.Commit();
					return true;
				}
				catch (Exception)
				{
					if (transaction.HasStarted())
					{
						transaction.RollBack();
					}
					return false;
				}
				return false;
			}
			return false;
		}

		public static bool ReadDataFromDrain(string inputGuid, Group DrainGroup, string strSchemaName, out DrainBuryDataStruct buryData)
		{
			buryData = new DrainBuryDataStruct();
			Guid guid = new Guid(inputGuid);
			Document document = DrainGroup.Document;
			Schema schema = Schema.Lookup(guid);
			Type typeFromHandle = typeof(DrainBuryDataStruct);
			if (schema == null || !schema.SchemaName.Equals(strSchemaName))
			{
				return false;
			}
			Entity entity = DrainGroup.GetEntity(schema);
			if (entity != null && entity.Schema != null)
			{
				foreach (FieldInfo fieldInfo in typeFromHandle.GetFields())
				{
					Field field = schema.GetField(fieldInfo.Name);
					Type fieldType = fieldInfo.FieldType;
					IList<string> value = new List<string>();
					if (fieldType == typeof(string))
					{
						string value2 = entity.Get<string>(field);
						fieldInfo.SetValue(buryData, value2);
					}
					else if (fieldType == typeof(List<string>))
					{
						value = entity.Get<IList<string>>(field);
						fieldInfo.SetValue(buryData, value);
					}
				}
				return true;
			}
			return false;
		}
	}
}
